package com.example.deertrip.ui.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.deertrip.Flight_Model_Activity;
import com.example.deertrip.Hotel_Model_Activity;
import com.example.deertrip.Moment_Detail_Activity;
import com.example.deertrip.MyImageLoader;
import com.example.deertrip.R;
import com.example.deertrip.SearchResult_Activity;
import com.example.deertrip.bean.Moment;
import com.example.deertrip.database.DBOperate;
import com.example.deertrip.databinding.FragmentHomeBinding;
import com.example.deertrip.tools_class.DrawableHelp;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    private Banner mbanner;
    private MyImageLoader myImageLoader;
    private ArrayList<Integer> photos;
    Button button_refresh;
   Button button_hotel;
   Button button_flight;
   ImageButton button_search;
   EditText input;
    SimpleAdapter adapter = null;
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
   private ListView listView;
    List<HashMap<String, Object>> data= new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
   private DBOperate dbOperate;




    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        button_hotel=root.findViewById(R.id.home_bt2);
        button_flight=root.findViewById(R.id.home_bt1);
        button_search=root.findViewById(R.id.search_button);
        listView=root.findViewById(R.id.home_list);
        mbanner=root.findViewById(R.id.banner);
        input=root.findViewById(R.id.search_input);
        button_hotel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), Hotel_Model_Activity.class);
                startActivity(intent);
            }
        });


        button_flight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), Flight_Model_Activity.class);
                startActivity(intent);
            }

        });

        button_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), SearchResult_Activity.class);
                intent.putExtra("search",input.getText().toString());
                startActivity(intent);
            }
        });


        dbOperate=new DBOperate(this.getContext());
        initData();
        initView();
        sql();

        return root;
    }



    private void sql(){


        List<Moment> moments= dbOperate.get_Hot_Moment_Data();

        Drawable dw;

        for(int i=0;i<moments.size();i++){
            HashMap<String, Object> items=new HashMap<String, Object>(); //存放一条数据的哈希表
            Moment moment=moments.get(i);
            BitmapDrawable bd=new BitmapDrawable(this.getResources(),moment.getImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
            dw=bd;    //再转换为Drawable
            items.put("image", dw);   //属性分别存入哈希表
            items.put("content", moment.getContent());
            items.put("username", moment.getUsername());
            items.put("good", moment.getGood());
            items.put("id",moment.get_Id());
            data.add(items);   //总数据哈希表中加入该条数据项
        }



        adapter= new SimpleAdapter(this.getContext(), data, R.layout.moment,
                new String[]{"image","content", "username","good"}, new int[]{R.id.moment_image,R.id.moment_content, R.id.moment_name, R.id.moment_good})
        {
            @Override
            public View getView(int position,View convertView,ViewGroup parent){
                final int p=position;
                Button goodbtn;// id为你自定义布局中按钮的id
                TextView goodtv;
                 convertView = super.getView(position, convertView, parent);

                  goodtv = convertView.findViewById(R.id.moment_good);
                 goodbtn = convertView.findViewById(R.id.moment_good_button);

                goodbtn.setOnClickListener(new View.OnClickListener()
                {

                    @Override
                    public void onClick(View v )
                    {
                        HashMap<String, Object> items=new HashMap<String, Object>();
                        items=(HashMap<String, Object>) getItem(p);
                        Integer id= (Integer) items.get("id");
                        dbOperate.GoodAdd(id);
                        Integer good=(Integer) items.get("good")+1;
                        items.put("good",good);
                        data.add(p,items);
                        String str=good.toString();
                        goodtv.setText(str);
                    }
                });
                return convertView;
            }
        };

        adapter.setViewBinder(new SimpleAdapter.ViewBinder(){
                     public boolean setViewValue(View view,Object data,String textRepresentation){
                                 if(view instanceof ImageView && data instanceof Drawable){
                                        ImageView iv=(ImageView)view;
                                         iv.setImageDrawable((Drawable)data);
                                         return true;
                                     }
                                else return false;
                             }
                 });

        listView.setAdapter(adapter);
        
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HashMap<String, Object> items=new HashMap<String, Object>();
                Bundle bundle=new Bundle();
                items=data.get(i);
                bundle.putString("username",(String) items.get("username"));
                bundle.putString("content",(String)items.get("content"));
                bundle.putInt("good",(int)items.get("good"));

                Drawable image=(Drawable) items.get("image");
                byte[] out= DrawableHelp.drawableTobyte(image);

               Intent intent=new Intent(getActivity(), Moment_Detail_Activity.class);
               intent.putExtras(bundle);
               intent.putExtra("image",out);
               startActivity(intent);
            }
        });

   }




    private void initView() {
        //设置轮播的样式
        mbanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);  //CIRCLE_INDICATOR
        //设置图片加载器
        mbanner.setImageLoader(myImageLoader);
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        mbanner.setBannerAnimation(Transformer.Accordion);
        //设置轮播间隔时间
        mbanner.setDelayTime(2500);
        //设置是否为自动轮播，默认是true
        mbanner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        mbanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载地址
        mbanner.setImages(photos).start();
                //开始调用的方法，启动轮播图。


    }

    private void initData() {
        myImageLoader = new MyImageLoader();
        photos = new ArrayList<Integer>();
        photos.add(R.drawable.shan1);
        photos.add(R.drawable.shu1);
        photos.add(R.drawable.shui1);
        photos.add(R.drawable.shui2);
        photos.add(R.drawable.sky1);

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }



}